package com.wbt.bambootown.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wbt.bambootown.R;
import com.wbt.bambootown.cons.Cons;
import com.wbt.bambootown.entity.ControlLog;
import com.wbt.bambootown.mvp.contract.ControlLogContract;
import com.wbt.bambootown.mvp.frame.MvpActivity;
import com.wbt.bambootown.mvp.frame.base.BaseView;
import com.wbt.bambootown.mvp.presenter.ControlLogPresenter;
import com.wbt.bambootown.ui.adapter.CameraPicAdapter;
import com.wbt.bambootown.ui.adapter.ControlLogAdapter;
import com.wbt.bambootown.utils.ActivityManagerUtil;
import com.wbt.bambootown.utils.EventCenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ControlLogActivity extends MvpActivity<ControlLogPresenter> implements ControlLogContract.View {
    @BindView(R.id.rv_log)
    XRecyclerView mRvLog;

    private int devicesId;
    private int pageIndex = 1;
    private int pageSize = 10;
    private ControlLogAdapter mAdapter;
    private boolean isFresh;

    @Override
    public void showError(String errorMsg) {
        dismissProgressDialog();
        Toast.makeText(this , errorMsg , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLogSuccess(List<ControlLog> controlLogs) {
        dismissProgressDialog();
        if(isFresh){
            mAdapter.setList(controlLogs);
            isFresh = false;
            mRvLog.refreshComplete();
        }else{
            mAdapter.appendList(controlLogs);
            mRvLog.loadMoreComplete();
        }
    }

    @OnClick(R.id.iv_back)
    public void onClick(){
        ActivityManagerUtil.create().finishActivity();
    }

    @Override
    protected BaseView getBaseView() {
        return this;
    }

    @Override
    public void initStatusBar(ImmersionBar immersionBar) {
        immersionBar
                .fitsSystemWindows(true)
                .statusBarColor(R.color.theme_color)
                .init();
    }

    @Override
    public void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mAdapter = new ControlLogAdapter(this);
        mRvLog.setLayoutManager(layoutManager);
        mRvLog.setAdapter(mAdapter);

        devicesId = getIntent().getIntExtra(Cons.SPCons.LIGHT_DEVICES_ID, -1);
        mPresenter.getControlLog2(devicesId ,  pageIndex , pageSize , "" , "");
        showProgressDialog();

        mRvLog.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isFresh = true;
                pageIndex = 1;
                mPresenter.getControlLog2(devicesId ,  pageIndex , pageSize , "" , "");
            }

            @Override
            public void onLoadMore() {
                pageIndex++;
                mPresenter.getControlLog2(devicesId ,  pageIndex , pageSize , "" , "");
            }
        });
    }

    @Override
    public void initData() {

    }

    @Override
    public Object getContentView() {
        return R.layout.activity_control_log;
    }

    @Override
    public boolean isApplyEventBus() {
        return false;
    }

    @Override
    public boolean isApplyButterKnife() {
        return true;
    }

    @Override
    public void onEventCome(EventCenter eventCenter) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mRvLog != null){
            mRvLog.destroy();
            mRvLog = null;
        }
    }
}
